package io.gitee.chearnee.fw.redis.serializer;

import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2023/9/6 18:22
 */
public class HKeyRedisSerializer implements RedisSerializer<Object> {
    private final Charset charset;

    public static final HKeyRedisSerializer UTF_8 = new HKeyRedisSerializer(StandardCharsets.UTF_8);

    public HKeyRedisSerializer(Charset charset) {
        this.charset = charset;
    }

    @Override
    public byte[] serialize(Object o) throws SerializationException {
        return (o == null ? null : o.toString().getBytes(charset));
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        return (bytes == null ? null : new String(bytes, charset));
    }
}
